/*
 * @Author: wangwei wwdqq7@qq.com
 * @Date: 2025-04-01 14:00:00
 * @LastEditors: wangwei wwdqq7@qq.com
 * @LastEditTime: 2025-04-01 15:13:05
 * @FilePath: /FullStack/pro/typeorm-mysql/src/user/permission.controller.ts
 * @Description: 权限控制器，提供权限管理的API接口
 * @- 权限的CRUD操作
 * @- 获取拥有特定权限的用户和角色
 */
import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  ParseIntPipe,
  Post,
  Put,
} from '@nestjs/common';
import {
  ApiBody,
  ApiOperation,
  ApiParam,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';

import { PermissionService } from '@/user/permission.service';

import {
  CreatePermissionDto,
  PermissionResponseDto,
  UpdatePermissionDto,
} from '../dto/permission.dto';

@ApiTags('权限管理')
@Controller('permission')
export class PermissionController {
  constructor(private permissionService: PermissionService) {}

  @ApiOperation({
    summary: '获取所有权限',
    description: '获取系统中所有权限的列表',
  })
  @ApiResponse({
    status: 200,
    description: '成功获取权限列表',
    type: [PermissionResponseDto],
  })
  @Get()
  async getPermissions() {
    return this.permissionService.getPermissions();
  }

  @ApiOperation({
    summary: '获取指定权限',
    description: '根据权限ID获取权限信息',
  })
  @ApiParam({ name: 'id', description: '权限ID' })
  @ApiResponse({
    status: 200,
    description: '成功获取权限信息',
    type: PermissionResponseDto,
  })
  @Get(':id')
  async getPermissionById(@Param('id', ParseIntPipe) id: number) {
    return this.permissionService.getPermissionById(id);
  }

  @ApiOperation({ summary: '创建新权限', description: '创建一个新的权限' })
  @ApiBody({ type: CreatePermissionDto })
  @ApiResponse({
    status: 201,
    description: '权限创建成功',
    type: PermissionResponseDto,
  })
  @Post()
  @HttpCode(HttpStatus.CREATED)
  async createPermission(@Body() permissionData: CreatePermissionDto) {
    return this.permissionService.createPermission(permissionData);
  }

  @ApiOperation({ summary: '更新权限信息', description: '更新指定权限的信息' })
  @ApiParam({ name: 'id', description: '权限ID' })
  @ApiBody({ type: UpdatePermissionDto })
  @ApiResponse({
    status: 200,
    description: '权限信息更新成功',
    type: PermissionResponseDto,
  })
  @Put(':id')
  async updatePermission(
    @Param('id', ParseIntPipe) id: number,
    @Body() permissionData: UpdatePermissionDto,
  ) {
    return this.permissionService.updatePermission(id, permissionData);
  }

  @ApiOperation({ summary: '删除权限', description: '删除指定的权限' })
  @ApiParam({ name: 'id', description: '权限ID' })
  @ApiResponse({ status: 200, description: '权限删除成功' })
  @Delete(':id')
  async deletePermission(@Param('id', ParseIntPipe) id: number) {
    return this.permissionService.deletePermission(id);
  }

  @ApiOperation({
    summary: '获取拥有权限的用户',
    description: '获取拥有指定权限的所有用户',
  })
  @ApiParam({ name: 'id', description: '权限ID' })
  @ApiResponse({ status: 200, description: '成功获取用户列表' })
  @Get(':id/users')
  async getPermissionUsers(@Param('id', ParseIntPipe) id: number) {
    return this.permissionService.getPermissionUsers(id);
  }

  @ApiOperation({
    summary: '获取拥有权限的角色',
    description: '获取拥有指定权限的所有角色',
  })
  @ApiParam({ name: 'id', description: '权限ID' })
  @ApiResponse({ status: 200, description: '成功获取角色列表' })
  @Get(':id/roles')
  async getPermissionRoles(@Param('id', ParseIntPipe) id: number) {
    return this.permissionService.getPermissionRoles(id);
  }
}
